package spider;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class stockSpider {


    public static void main(String[] args) throws IOException, InterruptedException {

        for(int i=1;i<=231;i++) {
            Thread.sleep(2000);
            String html = getIndex("http://q.10jqka.com.cn/index/index/board/all/field/zdf/order/desc/page/" + i + "/ajax/1/");

            System.out.println(html);
            Document doc = Jsoup.parse(html);
            Elements trs = doc.select("body > table > tbody > tr");
//            System.out.println(trs);
            for (Element tr : trs) {
//                System.out.println(tr);
//                System.out.println(tr.select("td").text());
                new FileOutputStream(new File("E:\\Desktop\\Stock.txt"),true)
                        .write((tr.select("td").text()+"\n").getBytes(StandardCharsets.UTF_8));
            }
        }

    }


    public static String getIndex(String url) throws IOException {
        //创建客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
//         setConnectTimeout(10000)连接超时时间（单位豪秒）
//         setSocketTimeout(10000)读取超时时间（单位豪秒）
        RequestConfig config = RequestConfig.custom().setConnectTimeout(2000)
                .setSocketTimeout(3000).build();
        httpGet.setConfig(config);
        httpGet.setHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36");

        httpGet.setHeader("Cookie",
                "spversion=20130314; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1645268892,1647621738; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1647622512; historystock=300142%7C*%7C300100%7C*%7C300001%7C*%7C300798; v=A89aHm_g8ePizfWP-b-g5WqZXmjc9CNbvUgnC-Hcaz5FsOEW6cSzZs0Ys1Dy");

        // 执行
        CloseableHttpResponse res = httpClient.execute(httpGet);
        HttpEntity entity = res.getEntity();
        String html = "";
        if (entity != null) {
            html = EntityUtils.toString(entity, Charset.forName("gb2312"));
        }

        return html;
    }

}
